package uc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.onekit.SCREEN;
import cn.onekit.SIZE;
import cn.onekit.android.ACTIVITY;

@SuppressWarnings("deprecation")
public class CircleGallery extends Gallery {
	static final float MIN_SCALE = 0.8F;

	public CircleGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	Bitmap[] images;
	String[] titles;

	public void setData(final String[] titles, final Bitmap[] images) {
		this.titles = titles;
		this.images = images;

		this.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return images.length;
			}

			@Override
			public Object getItem(int position) {
				return images[position];
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				SIZE size = SCREEN.size();
				LinearLayout viewGroup = new LinearLayout(getContext());
				viewGroup.setOrientation(LinearLayout.VERTICAL);
				viewGroup.setLayoutParams(new LayoutParams(size.width / 2, size.height / 2));
				if (position > 0) {
					viewGroup.setScaleX(MIN_SCALE);
					viewGroup.setScaleY(MIN_SCALE);
				}
				//
				ImageView imageView = new ImageView(getContext());
				LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				imageViewLayoutParams.weight = 1;
				imageView.setLayoutParams(imageViewLayoutParams);
				imageView.setPadding(ACTIVITY.dp2px(4), 0, 0, ACTIVITY.dp2px(4));
				imageView.setScaleType(ScaleType.CENTER_CROP);
				imageView.setImageBitmap(images[position]);
				viewGroup.addView(imageView);
				//
				TextView textView = new TextView(getContext());
				LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(50));
				textViewLayoutParams.weight = 0;
				textView.setLayoutParams(textViewLayoutParams);
				textView.setGravity(Gravity.CENTER);
			    textView.setTextColor(Color.WHITE);
				textView.setTextSize(25);
				textView.setText(titles[position]);
				viewGroup.addView(textView);
				//
				return viewGroup;
			}

		});
	}

	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (this.getChildCount() > 0) {
			final int WIDTH = ((View) this.getParent()).getWidth();
			final View first = this.getChildAt(0);
			final int width = first.getWidth();
			final int left0 = first.getLeft();
			final int index0 = this.getFirstVisiblePosition();
			final int index8 = this.getLastVisiblePosition();
			final int offset = (WIDTH - width) / 2 - left0 + index0 * width;
			for (int index = index0; index <= index8; index++) {
				final View view = this.getChildAt(index - index0);
				int left = offset - width * index;
				left = Math.min(width, left);
				left = Math.max(-width, left);
				float k = 1 - Math.abs(left) * (1 - MIN_SCALE) / width;
				view.setScaleX(k);
				view.setScaleY(k);
			}
			/*
			 * if (scrollViewListener != null) {
			 * scrollViewListener.onScrollChanged(this, offset); }
			 */
		}
	}
	/*
	 * ScrollViewListener scrollViewListener;
	 * 
	 * public void setOnScrollViewListener(ScrollViewListener
	 * scrollViewListener) { this.scrollViewListener = scrollViewListener; }
	 * 
	 * public interface ScrollViewListener {
	 * 
	 * void onScrollChanged(CircleGallery scrollView, int offset);
	 * 
	 * }
	 */
}
